package com.lxk.service.impl;

import com.lxk.dao.AppointMapper;
import com.lxk.dao.AsMyMapper;
import com.lxk.dao.DesignerMapper;
import com.lxk.dao.MeasureMapper;
import com.lxk.pojo.*;
import com.lxk.service.BalconySevice;
import com.lxk.vo.AppointMeasureModel;
import com.lxk.vo.AppointModel;
import com.lxk.vo.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author JohnSoybean
 * @create 2020/4/9 10:09
 * @description
 */
@Service
@Transactional
public class BalconyServiceImpl implements BalconySevice {

    @Autowired
    private AsMyMapper mm;
    @Autowired
    private DesignerMapper dm;
    @Autowired
    private AppointMapper am;
    @Autowired
    private MeasureMapper measureMapper;

    public Integer getDesignerPages(Integer num) {
        DesignerExample example = new DesignerExample();
        Integer count = (int)dm.countByExample(example);
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<Designer> selDesignerPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order) {
        int start = (pageIndex-1)*num;
        List<Designer> list = mm.selDesignerPage(start, num, sort, order);
        PageModel<Designer> model = new PageModel<Designer>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public Designer selDesignerById(Integer designerId) {
        Designer designer = dm.selectByPrimaryKey(designerId);
        return designer;
    }

    public void deleteDesigner(Integer designerId) {
        dm.deleteByPrimaryKey(designerId);
    }

    public void addDesigner(Designer designer) {
        dm.insertSelective(designer);
    }

    public void modifyDesigner(Designer designer) {
        dm.updateByPrimaryKeySelective(designer);
    }

    public Integer getAppointPages(Integer num) {
        AppointExample example = new AppointExample();
        Integer count = (int)am.countByExample(example);
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<AppointModel> selAppointPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order) {
        int start = (pageIndex-1)*num;
        List<AppointModel> list = mm.selAppointPage(start, num, sort, order);
        PageModel<AppointModel> model = new PageModel<AppointModel>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public Appoint selAppointById(Integer appointId) {
        Appoint appoint = am.selectByPrimaryKey(appointId);
        return appoint;
    }

    public void handleAppoint(Integer appointId) {
        Appoint appoint = new Appoint();
        appoint.setAppointId(appointId);
        appoint.setIsFinished(1);
        am.updateByPrimaryKeySelective(appoint);
    }

    public Integer getMeasureAppointPages(Integer num) {
        MeasureExample example = new MeasureExample();
        Integer count = (int)measureMapper.countByExample(example);
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<AppointMeasureModel> selMeasureAppointPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order) {
        int start = (pageIndex-1)*num;
        List<AppointMeasureModel> list = mm.selAppointMeasurePage(start, num, sort, order);
        PageModel<AppointMeasureModel> model = new PageModel<AppointMeasureModel>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public Measure selMeasureById(Integer measureId) {
        Measure measure = measureMapper.selectByPrimaryKey(measureId);
        return measure;
    }

    public void handleMeasure(Integer measureId) {
        Measure measure = new Measure();
        measure.setMeasureId(measureId);
        measure.setIsFinished(1);
        measureMapper.updateByPrimaryKeySelective(measure);
    }

}
